/* @(#)GSOperatorScanQueryAction.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-12    建立
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSDeliverOrderManufactureVO;

import com.hitrust.gs.vo.GSDeliverOrderSaveVO;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderProcesser;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.form.GSDeliverOrderSaveForm;
import com.hitrust.gs.util.lang.IDGeneratorDeliver;

/**
 * @author simonyi
 *
 * 订单保存
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSDeliverOrderSaveAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
	    
        GSOperatorBean bean = (GSOperatorBean) aForm.getSession().getAttribute(
                GSBaseConstants.LOGIN);

	    GSDeliverOrderSaveForm tForm = (GSDeliverOrderSaveForm)aForm;
	    
	    GSDeliverOrderSaveVO orderTempvo = new GSDeliverOrderSaveVO();
	    GSManufactureProcessor proc = new GSManufactureProcessor();
	    GSManufactureDAO md = new GSManufactureDAO();
	    
	    
	    //ordervo.orderid = (String)request.getParameter("orderid");
	    orderTempvo.gs_balance_mode = "2".equals(tForm.getBalancemode())?tForm.getBalancemode_():tForm.getBalancemode();
	    orderTempvo.gs_deliver_mode = "2".equals(tForm.getDelivermode())?tForm.getDelivermode_():tForm.getDelivermode();
	    orderTempvo.gs_deliver_goods_addr = tForm.getDeliveraddr();
	    orderTempvo.gs_workcorp_id = tForm.getDeptid();
	    orderTempvo.gs_deliveryman = tForm.getLinkman_()==null?"":tForm.getLinkman_();
	    orderTempvo.gs_deliver_goods_phone = tForm.getLinktel_()==null?"":tForm.getLinktel_();
	    orderTempvo.gs_notes = tForm.getNotes();

	    
	    orderTempvo.operId = bean.getGs_oper_id();
	    orderTempvo.orgId = bean.getGs_oper_org_id();
	    
	    ArrayList ary = new ArrayList();
	    
	    String[] manufactureName = tForm.getManufacturename();
	    String[] spec = tForm.getSpec();
	    String[] type = tForm.getType();
	    String[] brand = tForm.getBrand();
	    String[] manufacturer = tForm.getManufacturer();
	    String[] orderCount = tForm.getOrdercount();
	    String[] quantUnit = tForm.getQuantunit();
	    String[] signinPrice = tForm.getSigninprice();
	    String[] otherProperties = tForm.getOtherproperties();
	    String[] mode=tForm.getMode();
	    String providerId = tForm.getProviderid();
	    String deliverGoodsId = tForm.getDelivergoodsid();
	    String signinDate = tForm.getSignindate();
	    String signinPerson = tForm.getSigninperson();
	    
	    try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createDate = sdf.format(new Date(System.currentTimeMillis()));

	        for(int i=0;i<10;i++){
		        
		        if(manufactureName[i] == null || manufactureName[i].length() == 0)
		            continue;

		        //先查询产品编号是否存在
	            String manufactureId = proc.getManufactureIdForDeliverOrder(manufactureName[i],spec[i],type[i],brand[i],quantUnit[i],signinPrice[i]);
	            if(manufactureId == null){
	                GSManufactureBean mbean = new GSManufactureBean();
	                IDGeneratorDeliver idg = new IDGeneratorDeliver();
	                manufactureId = "SI"+idg.formatID(idg.generatorManufactureID(),8);
	                mbean.setGs_manufacture_id(manufactureId);
	                mbean.setGs_concentricity(manufactureId);
	                mbean.setGs_manufacture_name(manufactureName[i]);
	                mbean.setGs_manufacture_category_id("90000");
	                mbean.setGs_manufacture_unitprice(Double.parseDouble("".equals(signinPrice[i])?"0":signinPrice[i]));
	                mbean.setGs_quant_unit(quantUnit[i]);
	                mbean.setGs_provider_id(providerId);
	                mbean.setGs_manufacture_record_date(createDate);
	                mbean.setGs_is_tradeflat_manufacture("2");
	                mbean.setGs_manufacture_spec(spec[i]);
	                mbean.setGs_manufacture_type(type[i]);
	                mbean.setGs_manufacture_brand(brand[i]);
	                mbean.setGs_manufacturer_name(manufacturer[i]);
	                mbean.setGs_appender_property(otherProperties[i]);
	                mbean.setGs_balance_mode(mode[i]);
	                md.add(mbean);
	            }
		        
		        ary.add(new String[]{manufactureId,orderCount[i],providerId,deliverGoodsId,signinPrice[i],signinDate,signinPerson,otherProperties[i],mode[i]});
		    }
	        
	    }catch(Exception e){
	        GSLogWriter.error(this,"为送货单保存产品信息时发生错误");
	        aForm.getRequest().setAttribute("action", "A110101.do");
			throw e;	
	    }
	    
	    GSOrderProcessor processor = new GSOrderProcessor();
	    GSOrderProcesser processer = new GSOrderProcesser();
	    try{
	        String orderId = processer.saveDeliverOrder(bean,orderTempvo,ary);//"010000713L";
	        
	        String saveOption = aForm.getRequest().getParameter("saveoption");
	        if("SA".equals(saveOption)){
	            aForm.getRequest().setAttribute("SUCCESSMSG","送货单保存成功，编号为 "+orderId);
	            return "new";//允许录入新送货单
	        }
	        if("S".equals(saveOption)){
		        ArrayList arylist = processor.getDeliverOrderDetail(orderId);
		        GSOrderBean tBean = processor.selectOrderByCode(orderId);
		        
		        aForm.getRequest().setAttribute("bean",tBean);
		        aForm.getRequest().setAttribute("arylist",arylist);
		        aForm.getRequest().setAttribute("SUCCESSMSG","送货单保存成功，编号为 "+orderId);
		        return GSBaseConstants.FORWARD_SUCCESS;
	        }
	        return GSBaseConstants.FORWARD_FAILURE;
	    }
	    catch(GSException e){
			GSLogWriter.error(this, e.getMessage());
			aForm.getRequest().setAttribute("action", "A110101.do");
			throw e;	        
	    }
	    
	}
}
